set -e

setup_go_dist () {
    VERSION_MAJOR_MINOR=$1
    VERSION_RELEASE=$2
    VERSION_FULL="${1}.${2}"

    FILE=go${VERSION_FULL}.linux-amd64.tar.gz
    [ ! -e "${FILE}" ] && wget -nv https://dl.google.com/go/${FILE}

    mkdir -p /usr/local/goFiles${VERSION_MAJOR_MINOR}.x
    tar -xf /tmp/${FILE}
    mv /tmp/go /usr/local/go${VERSION_MAJOR_MINOR}.x
}

test_go_dist () {
    VERSION_MAJOR_MINOR=$1

    echo "testing go-gci Go ${VERSION_MAJOR_MINOR}.x"
    export PATH=/usr/local/go${VERSION_MAJOR_MINOR}.x/bin:${PATH_SAVE}
    export GOROOT=/usr/local/go${VERSION_MAJOR_MINOR}.x
    export GOPATH=/usr/local/goFiles${VERSION_MAJOR_MINOR}.x
    mkdir -p ${GOPATH}/src/gitee.com/GBase8s/go-gci
    cp -r ${TESTDIR}/* ${GOPATH}/src/gitee.com/GBase8s/go-gci/

    go test -v gitee.com/GBase8s/go-gci -args -disableDatabase=false -hostValid ${DOCKER_IP} -username scott -password tiger
}

echo "installing build tools"
apt-get -qq -y update  2>&1 > /dev/null
apt-get -qq -y install git pkg-config gcc wget  2>&1 > /dev/null

echo "installing go"
cd /tmp/

setup_go_dist "1.15" "6"
setup_go_dist "1.14" "13"
setup_go_dist "1.13" "15"
setup_go_dist "1.12" "17"
setup_go_dist "1.11" "13"
setup_go_dist "1.10" "8"
setup_go_dist "1.9" "7"


export GBASEDBTDIR=
export PATH=$GBASEDBTDIR/bin:$PATH
export GBASEDBTSERVER=
export GCICLIENTDIR=
export LD_LIBRARY_PATH=

DOCKER_IP=$(ifconfig eth0 | awk '/inet / { printf $2; exit }')

tnsping ${DOCKER_IP}

echo "creating gbase8s.pc"
mkdir -p /usr/local/pkg_config
cd /usr/local/pkg_config
export PKG_CONFIG_PATH=/usr/local/pkg_config
cat > gbase8s.pc <<PKGCONFIG
Name: gbase8s
Description: Call Interface
Version: V8.8
Cflags: -I${GCICLIENTDIR}/include
Libs: -L${GCICLIENTDIR}/lib -lgci
PKGCONFIG


export PATH_SAVE=${PATH}

test_go_dist "1.15"
test_go_dist "1.14"
test_go_dist "1.13"
test_go_dist "1.12"
test_go_dist "1.11"
test_go_dist "1.10"
test_go_dist "1.9"
